## Boost (headers) integration
# Prefer Config package from a vendored Boost under ${DEP_PLATFORM_DIR}/boost, with graceful fallback

option(USE_BOOST "Use Boost headers" ON)
if(NOT USE_BOOST)
    return()
endif()

# Local (vendored) Boost root hint
set(BOOST_ROOT_LOCAL "${DEP_PLATFORM_DIR}/boost")

# If a local Boost exists, help CMake find its config package
if(EXISTS "${BOOST_ROOT_LOCAL}/lib/cmake")
    list(PREPEND CMAKE_PREFIX_PATH "${BOOST_ROOT_LOCAL}")
endif()
message(STATUS "Boost integration: DEP_PLATFORM='${DEP_PLATFORM_DIR}', BOOST_ROOT_LOCAL='${BOOST_ROOT_LOCAL}'")

# Try Config mode first (Boost >= 1.70 provides Boost::headers), then module mode
set(_boost_found FALSE)

# Try config-mode first
find_package(Boost CONFIG QUIET)
if(Boost_FOUND)
    set(_boost_found TRUE)
    if(TARGET Boost::headers)
        get_target_property(_boost_inc Boost::headers INTERFACE_INCLUDE_DIRECTORIES)
        get_filename_component(_boost_inc_norm "${_boost_inc}" REALPATH)
        # Some packages point to a root that contains a nested boost/include tree.
        if(_boost_inc_norm AND EXISTS "${_boost_inc_norm}/include/boost")
            set(_boost_inc_corrected "${_boost_inc_norm}/include")
            set_property(TARGET Boost::headers PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${_boost_inc_corrected}")
            message(STATUS "Found Boost (config): adjusted Boost::headers includes to '${_boost_inc_corrected}' (from '${_boost_inc_norm}')")
        elseif(_boost_inc_norm AND EXISTS "${_boost_inc_norm}/boost/include/boost")
            set(_boost_inc_corrected "${_boost_inc_norm}/boost/include")
            set_property(TARGET Boost::headers PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${_boost_inc_corrected}")
            message(STATUS "Found Boost (config): adjusted Boost::headers includes to '${_boost_inc_corrected}' (from '${_boost_inc_norm}')")
        else()
            message(STATUS "Found Boost (config): using Boost::headers; includes='${_boost_inc_norm}'")
        endif()
        # refresh after potential correction
        get_target_property(_boost_inc Boost::headers INTERFACE_INCLUDE_DIRECTORIES)
    else()
        if(DEFINED Boost_INCLUDE_DIRS)
            message(STATUS "Found Boost (config): includes='${Boost_INCLUDE_DIRS}' (no Boost::headers target)")
        else()
            message(STATUS "Found Boost (config): no include dirs reported by package")
        endif()
    endif()
else()
    # Fallback to module-mode find
    find_package(Boost QUIET)
    if(Boost_FOUND)
        set(_boost_found TRUE)
        if(DEFINED Boost_INCLUDE_DIRS)
            message(STATUS "Found Boost (module): includes='${Boost_INCLUDE_DIRS}'")
        else()
            message(STATUS "Found Boost (module): include dirs not reported")
        endif()
    endif()
endif()

# Fallback: construct a header-only imported target if not found but headers exist
if(NOT _boost_found)
    if(EXISTS "${BOOST_ROOT_LOCAL}/include/boost" )
        set(Boost_INCLUDE_DIRS "${BOOST_ROOT_LOCAL}/include")
        set(Boost_FOUND TRUE)
        set(_boost_found TRUE)
        if(NOT TARGET Boost::headers)
            add_library(Boost::headers INTERFACE IMPORTED)
            target_include_directories(Boost::headers INTERFACE "${Boost_INCLUDE_DIRS}")
        endif()
        message(STATUS "Using vendored Boost headers: ${Boost_INCLUDE_DIRS}")
    else()
        message(WARNING "Boost not found. Install Boost or place it under ${BOOST_ROOT_LOCAL}")
    endif()
endif()

# If Config or module mode found Boost but no Boost::headers target, synthesize it from include dirs
if(_boost_found AND NOT TARGET Boost::headers)
    if(DEFINED Boost_INCLUDE_DIRS)
        add_library(Boost::headers INTERFACE IMPORTED)
        target_include_directories(Boost::headers INTERFACE ${Boost_INCLUDE_DIRS})
        message(STATUS "Created Boost::headers from Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
    endif()
endif()

# Export common variables and set global includes for consumers that don't link Boost::headers
set(Boost_FOUND ${_boost_found} PARENT_SCOPE)
if(DEFINED Boost_INCLUDE_DIRS)
    set(Boost_INCLUDE_DIRS ${Boost_INCLUDE_DIRS} PARENT_SCOPE)
endif()

# Ensure headers are visible globally to keep existing targets working
if(TARGET Boost::headers)
    get_target_property(_boost_inc Boost::headers INTERFACE_INCLUDE_DIRECTORIES)
    get_filename_component(_boost_inc_norm "${_boost_inc}" REALPATH)
    # Prefer an include path that ends with '/include' and contains 'boost'
    if(_boost_inc_norm AND EXISTS "${_boost_inc_norm}/include/boost")
        include_directories("${_boost_inc_norm}/include")
        message(STATUS "Boost global include directories: ${_boost_inc_norm}/include")
    elseif(_boost_inc_norm AND EXISTS "${_boost_inc_norm}/boost/include/boost")
        include_directories("${_boost_inc_norm}/boost/include")
        message(STATUS "Boost global include directories: ${_boost_inc_norm}/boost/include")
    elseif(_boost_inc_norm AND EXISTS "${_boost_inc_norm}/boost")
        # Last resort: if only '<norm>/boost' exists, point to its include subdir
        if(EXISTS "${_boost_inc_norm}/boost/include/boost")
            include_directories("${_boost_inc_norm}/boost/include")
            message(STATUS "Boost global include directories: ${_boost_inc_norm}/boost/include")
        else()
            include_directories("${_boost_inc_norm}")
            message(STATUS "Boost global include directories: ${_boost_inc_norm}")
        endif()
    else()
        include_directories("${_boost_inc_norm}")
        message(STATUS "Boost global include directories: ${_boost_inc_norm}")
    endif()
elseif(DEFINED Boost_INCLUDE_DIRS)
    include_directories(${Boost_INCLUDE_DIRS})
    message(STATUS "Boost global include directories: ${Boost_INCLUDE_DIRS}")
elseif(EXISTS "${BOOST_ROOT_LOCAL}/include/boost")
    include_directories("${BOOST_ROOT_LOCAL}/include")
    message(STATUS "Boost global include directories: ${BOOST_ROOT_LOCAL}/include")
endif()
